// Copyright (c) 2008 screamingtarget@gmail.com
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.


#pragma once

#include <boost/thread.hpp>
#include <boost/bind.hpp>
#include <boost/function.hpp>

#include "Server.h"


const UINT RWM_SINGLEINSTANCE = RegisterWindowMessage( _T("RWM_SINGLEINSTANCE__0EE3CDCF_C40B_4444_84FB_E8AF4481E594") );


class CMainDlg : public CDialogImpl< CMainDlg >, public CUpdateUI< CMainDlg >,
		public CMessageFilter, public CIdleHandler, public CWinDataExchange< CMainDlg >
{
public:

	enum { IDD = IDD_MAINDLG };

	virtual BOOL PreTranslateMessage( MSG* pMsg );
	virtual BOOL OnIdle();

	BEGIN_UPDATE_UI_MAP( CMainDlg )
	END_UPDATE_UI_MAP()

	BEGIN_MSG_MAP( CMainDlg )
		MESSAGE_HANDLER( WM_INITDIALOG, OnInitDialog )
		MESSAGE_HANDLER( WM_DESTROY, OnDestroy )
		COMMAND_ID_HANDLER( IDCANCEL, OnCancel )
      MESSAGE_HANDLER( RWM_SINGLEINSTANCE, OnSingleInstance )
      COMMAND_HANDLER( IDC_PORT, EN_UPDATE, OnPortUpdate )
      COMMAND_HANDLER( IDC_PORT, EN_KILLFOCUS, OnPortKillFocus )
      COMMAND_HANDLER( IDC_START, BN_CLICKED, OnStartClicked )
      MESSAGE_HANDLER( RWM_SERVERPROCACCEPT, OnServerProcAccept )
      MESSAGE_HANDLER( RWM_SERVERPROCEXIT, OnServerProcExit )
      COMMAND_HANDLER( IDC_STOP, BN_CLICKED, OnStopClicked )
      MESSAGE_HANDLER( RWM_CONNECTIONLOST, OnConnectionLost )
      MESSAGE_HANDLER( RWM_SERVERSTARTED, OnServerStarted )
      MESSAGE_HANDLER( RWM_PORTAUDIOSTATUS, OnPortAudioStatus )
	END_MSG_MAP()

   BEGIN_DDX_MAP( CMainDlg )
      DDX_CONTROL_HANDLE( IDC_IPADDRESSES, c_ipAddresses )
      DDX_CONTROL_HANDLE( IDC_PORT, c_port )
      DDX_CONTROL_HANDLE( IDC_LISTEN, c_start )
      DDX_CONTROL_HANDLE( IDC_STOP, c_stop )
      DDX_CONTROL_HANDLE( IDC_CONNECTED, c_connected )
      DDX_CONTROL_HANDLE( IDC_STATUS, c_status )
   END_DDX_MAP()

// Handler prototypes (uncomment arguments if needed):
//	LRESULT MessageHandler( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ )
//	LRESULT CommandHandler( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/ )
//	LRESULT NotifyHandler( int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/ )

private:

   CComboBox c_ipAddresses;
   CEdit c_port;
   CButton c_start;
   CButton c_stop;
   CStatic c_connected;
   CListBox c_status;

   boost::function< void() > StopServer_;

   enum
   {
      MinPort = 1,
      MaxPort = 65535
   };

	LRESULT OnInitDialog( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );
	LRESULT OnDestroy( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );
	LRESULT OnCancel( WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/ );

	void CloseDialog( int nVal );

   LRESULT OnSingleInstance( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );

   void PersistWindowPlacement();
   void PlaceWindow();

   void PersistSettings();
   bool RetrievePort();
   void PopulateIpAddresses( const StringVector& ipAddresses );

   void EnableListenButton();
   void EnableDisconnectButton();

   LRESULT OnPortUpdate( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/ );

   LRESULT OnPortKillFocus( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/ );

   LRESULT OnStartClicked( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/ );
   LRESULT OnStopClicked( WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/ );

   void EnableControls( BOOL enable = TRUE );

   void ServerProc( tcp::endpoint endpoint );

   LRESULT OnServerProcAccept( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );
   LRESULT OnServerProcExit( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );

   void SetConnectedText( bool connected = true );

   LRESULT OnServerStarted( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );
   LRESULT OnConnectionLost( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );

   LRESULT OnPortAudioStatus( UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/ );
};
